package com.laicunba.service.mq;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;

import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.JzhAccountDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.pojo.WithdrawDO;
import com.laicunba.pojo.vo.UserLoginTokenVO;
import com.laicunba.util.StringUtil;

@SuppressWarnings("unchecked")
public class ActiveMQMessageFactory {
	public final static String CORE_USR_PAY_SUCCESS = "lcb.core.usr.pay.success";
	public static final String CORE_USR_WITHDRAW_APPLY_SUCCESS = "lcb.core.usr.withdraw.apply.success";
	public static final String CORE_USR_H2D_FAIL = "lcb.core.usr.h2d.fail";

	private static JSONObject createNewMessage(String msgType) {
		JSONObject msg = new JSONObject();
		msg.put("MSG_ID", StringUtil.genGUID());
		msg.put("MSG_TYPE", msgType);
		msg.put("T", System.currentTimeMillis());
		return msg;
	}

	public static JSONObject createUsrCreateMessage(UserDO userDO) {
		JSONObject msg = createNewMessage("USR_CREATE");
		msg.putAll(toMap(userDO));
		msg.remove("loginPwd");
		return msg;
	}

	public static JSONObject createUsrLoginMessage(UserLoginTokenVO userToken) {
		JSONObject msg = createNewMessage("USR_LOGIN");
		msg.putAll(toMap(userToken));
		msg.remove("token");
		return msg;
	}

	public static JSONObject createUsrPaySuccessMessage(FinancingTargetBidDO bidDO) {
		JSONObject msg = createNewMessage("USR_PAY_SUCCESS");
		msg.putAll(toMap(bidDO));
		return msg;
	}

	public static JSONObject createEmptyMessage(String type) {
		JSONObject msg = createNewMessage(type); 
		return msg;
	}
	
	
	@SuppressWarnings("rawtypes")
	public static Map toMap(Object obj) {
		Map map = new HashMap();
		if (obj == null) {
			return map;
		}
		String methodName = "";
		Object val = "";
		for (Method method : obj.getClass().getMethods()) {
			methodName = method.getName();
			if (!methodName.startsWith("get") || "getClass".equals(methodName)) {
				continue;
			}
			methodName = methodName.substring(3);
			methodName = Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
			try {
				val = method.invoke(obj);
				if (val == null) {
					map.put(methodName, null);
				} else if (val instanceof Date) {
					map.put(methodName, ((Date) val).getTime());
				} else {
					map.put(methodName, val);
				}
			} catch (Exception e) {
			}
		}

		return map;
	}

	public static JSONObject createWithdrawMessage(WithdrawDO withdrawDO) {
		JSONObject msg = createNewMessage("WITHDRAW_DO");
		msg.putAll(toMap(withdrawDO));
		return msg;
	}

	public static JSONObject createBidMessage(FinancingTargetBidDO bidDO) {
		JSONObject msg = createNewMessage("BID_DO");
		msg.putAll(toMap(bidDO));
		return msg;
	}

	public static JSONObject createMessage(String type, Object obj) {
		JSONObject msg = createNewMessage(type);
		msg.putAll(toMap(obj));
		return msg;
	}

}
